<?php

namespace app\shop\controller\order;

use app\common\enum\order\OrderPayStatusEnum;
use app\shop\controller\Controller;
use app\shop\model\order\Order as OrderModel;
use app\shop\model\order\OrderRefund as OrderRefundModel;
use app\shop\model\settings\ReturnAddress as ReturnAddressModel;


/**
 * 售后管理
 */
class Refund extends Controller
{
    /**
     * 售后列表
     */
    public function indexBlack()
    {
        $model = new OrderRefundModel;
        $params = $this->postData();
        //列表数据
        $list = $model->getList($params);
        //重要数字
        $num_arr = $model->groupCount($params);
        $arr = [];
        foreach ($num_arr as $key => $val) {
            $k = $val['status']['value'];
            $arr[$k] = $val;
        }

        return $this->renderSuccess('', compact('list', 'arr'));
    }

    public function index()
    {
        $model = new OrderRefundModel;
        $params = $this->postData();
        $params['order_no'] = isset($params['order_no'])? trim($params['order_no']):'';
        //列表数据
        $list = $model->getList($params);
        //重要数字
        $num_arr = $model->groupCount($params);


        return $this->renderSuccess('', compact('list', 'num_arr'));
    }


    /**
     *待处理的售后订单
     * getRefundOrder
     * \think\response\Json
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @Author kevin
     * @date 2024/5/9 14:33
     */
    public function getRefundOrder()
    {
        $refundList = (new OrderModel())->where('is_order_refund', '=', 1)->select();
        $orderList = OrderModel::withJoin(['user'])
            ->where('pay_status', '=', OrderPayStatusEnum::SUCCESS)
            ->where('delivery_status', '=', 10)
            ->where('order_status', '=', 10)
            ->where(function ($query) {
                $query->where('assemble_status','=',0)->whereOr('assemble_status','=',20);
            })
            ->select();
        return $this->renderSuccess('', compact('refundList', 'orderList'));
    }

    /**
     * 售后单详情
     */
    public function detailBlack($order_refund_id)
    {
        // 售后单详情
        $detail = OrderRefundModel::detail($order_refund_id);
        if (isset($detail['send_time']) && $detail['send_time'] > 0) {
            $detail['send_time'] = date('Y-m-d H:i:s', $detail['send_time']);
        }
        // 订单详情
        $order = OrderModel::detail($detail['order_id']);
        $detail['orderproduct']['max_refund_money'] = $detail['orderproduct']['total_pay_price'];
        if ($order['order_source'] == 70) {
            $detail['orderproduct']['total_pay_price'] = round($detail['orderproduct']['total_pay_price'] + $order['advance']['pay_price'], 2);
            if ($order['advance']['money_return'] == 1) {
                $detail['orderproduct']['max_refund_money'] = round($detail['orderproduct']['max_refund_money'] + $order['advance']['pay_price'], 2);
            }
        }
        // 退货地址
        $address = (new ReturnAddressModel)->getAll($detail['shop_supplier_id']);
        return $this->renderSuccess('', compact('detail', 'order', 'address'));
    }

    public function detail($order_id)
    {

        // 售后单详情
        $detail = \app\api\model\order\Order::one(['order_id' => $order_id], ['address', 'product', 'orderRefund' => function ($query) {
            $query->with(['image.file']);
        }]);
        $refund_audit_time = '';
        if($detail['orderRefund']){
            foreach ($detail['orderRefund'] as  $v){
                $refund_audit_time = $v['audit_time']?date('Y-m-d H:i:s',$v['audit_time']):'';
            }
        }

        $detail['refund_audit_time'] = $refund_audit_time;
        return $this->renderSuccess('', compact('detail'));
    }

    /**
     * 商家审核
     */
    public function auditBlack($order_refund_id)
    {
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->audit($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    public function audit($order_id)
    {
        $model = new OrderRefundModel();
        if ($model->audit($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 确认收货并退款
     */
    public function receipt($order_refund_id)
    {
        if (!$this->request->isPost()) {
            return false;
        }
        $model = OrderRefundModel::detail($order_refund_id);
        if ($model->receipt($this->postData())) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

}
